package org.jeecg.modules.paycenter.channel.pufa;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.jeecg.modules.paycenter.dto.PayBillData;
import org.jeecg.modules.paycenter.service.BaseBankService;
import org.jeecg.modules.wlhy.bill.entity.*;
import org.jeecg.modules.wlhy.company.entity.HyCompany;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hypfaccount.entity.HyPfAccount;
import org.jeecg.modules.wlhy.hypfaccount.service.IHyPfAccountService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class PfBankService extends BaseBankService {
    public static  final String CHANNEL = PayChannelEnum.PF.channelType;

    @Autowired
    private IHyPfAccountService hyPfAccountService;

    public Result<?> getDriverAccounts(Map<String, HyDriver> driverMaps) {
        Map<String, HyPfAccount> driverAccountBookMaps = new HashMap<>();
        Set<String> driverSet = driverMaps.keySet();
        Collection<HyDriver> hyDrivers = driverMaps.values();

        List<HyPfAccount> driverAccounts =  hyPfAccountService.getDriverAccounts(driverSet);
        for (HyPfAccount account : driverAccounts) {
            driverAccountBookMaps.put(account.getUid(), account);
        }
        if (driverAccounts.size() != driverSet.size()) {
            List<String> noAccDriverList = new ArrayList<>();
            List<String> noAccDriverIdList = new ArrayList<>();
            for (HyDriver hyDriver : hyDrivers) {
                if (!driverAccountBookMaps.containsKey(hyDriver.getId())) {
                    noAccDriverList.add(hyDriver.getDriverName());
                    noAccDriverIdList.add(hyDriver.getId());
                }
            }
            return Result.error(-99,String.join(",", noAccDriverList) + ":等司机未开通钱包" , String.join(",", noAccDriverIdList));
        }
        return Result.OK(driverAccountBookMaps);
    }

    public Map<String, HyPfAccount> getCarrierAccounts(Map<String, HyCarrier> carrierMaps) {
        Map<String, HyPfAccount> carrierAccountMaps = new HashMap<>();//车队长钱包
        Set<String> carrierKeys = carrierMaps.keySet();
        if(carrierKeys.size() > 0) {
            for (String carrierId : carrierKeys) {
                HyPfAccount carrierAccount = hyPfAccountService.getCarrierAccount(carrierId);
                if (carrierAccount == null) {
                    HyCarrier hyCarrier = carrierMaps.get(carrierId);
                    throw new JeecgBootException("车队长:" + hyCarrier.getCarrierName() + ",未开通钱包");
                }
                carrierAccountMaps.put(carrierId, carrierAccount);
            }
        }
        return carrierAccountMaps;
    }

    public Map<String, HyPfAccount> getCompanyAccounts(Map<String, HyCompany> companyMaps) {
        Set<String> companySet = companyMaps.keySet();
        List<HyPfAccount> companyAccounts = hyPfAccountService.getCompanyAccounts(companySet);
        if (companyAccounts.size() != companySet.size()) {
            throw new JeecgBootException("分公司未开通浦发钱包");
        }
        Map<String, HyPfAccount> companyAccountBookMaps = new HashMap<>();
        for (HyPfAccount companyAccount : companyAccounts) {
            companyAccountBookMaps.put(companyAccount.getUid(),companyAccount);
        }
        return companyAccountBookMaps;
    }

    public PayBillData setBillPayData(PayBillData payBillData ,
                                      HyPfAccount companyAccountBook,
                                      HyPfAccount customerAccountBook,
                                      HyPfAccount driverAccountBook,
                                      HyPfAccount carrierAccountBook) {
        HyBill hyBill = payBillData.getHyBill();
        //货主钱包
        String customerDbAccNo = customerAccountBook.getChildAcctNo();
        String customerDbAccName = customerAccountBook.getChildAcctNm();
        //分公司子账号
        String companyAccNo = companyAccountBook.getChildAcctNo();
        String companyAccName = companyAccountBook.getChildAcctNm();
        //司机账簿
        String driverAccNo = driverAccountBook.getChildAcctNo();
        String driverAccName = driverAccountBook.getChildAcctNm();

        HyCustomerBill customerBill = payBillData.getCustomerBill();
        customerBill.setDbaccno(customerDbAccNo);
        customerBill.setDbaccname(customerDbAccName);
        customerBill.setDblogaccno("");
        customerBill.setDbProv("");
        customerBill.setCraccno(companyAccNo);
        customerBill.setCraccname(companyAccName);
        customerBill.setCrlogaccno("");
        customerBill.setCrProv("");


        HyShippingNoteBill shippingNoteBill = payBillData.getShippingNoteBill();
        //分公司
        shippingNoteBill.setDbaccno(companyAccNo);
        shippingNoteBill.setDbaccname(companyAccName);
        shippingNoteBill.setDblogaccno("");
        shippingNoteBill.setDbProv("");
        //添加公司支付司机流水单
        shippingNoteBill.setCraccno(driverAccNo);
        shippingNoteBill.setCraccname(driverAccName);
        shippingNoteBill.setCrlogaccno("");
        shippingNoteBill.setCrProv("");
        shippingNoteBill.setBankCode("");

        //添加司机流水单
        HyDriverBill driverBill = payBillData.getDriverBill();
        driverBill.setDbaccno(companyAccNo);
        driverBill.setDbaccname(companyAccName);
        driverBill.setDblogaccno("");
        driverBill.setCraccno(driverAccNo);
        driverBill.setCraccname(driverAccName);
        driverBill.setCrlogaccno("");

        //是否车队长代收
        if ("1".equals(hyBill.getIzCarrierPay())) {
            //添加车队长流水单 (司机支付给车队长)
            String carrierAccNo = carrierAccountBook.getChildAcctNo();
            String carrierAccName = carrierAccountBook.getChildAcctNm();

            HyCarrierBill carrierBill = payBillData.getCarrierBill();
            carrierBill.setDbaccno(driverAccNo);
            carrierBill.setDbaccname(driverAccName);
            carrierBill.setDblogaccno("");
            carrierBill.setDbProv("");
            carrierBill.setCraccno(carrierAccNo);
            carrierBill.setCraccname(carrierAccName);
            carrierBill.setCrlogaccno("");
            carrierBill.setCrBankCode("");
            payBillData.setCarrierBill(carrierBill);
        }
        return payBillData;
    }

    public void setBillPayDatasAccInfo(List<PayBillData> payDatas, HyPfAccount customerAccountBook, Map<String, HyPfAccount> companyAccountBookMaps, Map<String, HyPfAccount> driverAccountBookMaps, Map<String, HyPfAccount> carrierAccountBookHashMaps) {
        for (PayBillData payData : payDatas) {
            HyBill hyBill = payData.getHyBill();
            HyShippingNote hyShippingNote = payData.getShippingNote();
            HyPfAccount driverAccountBook = driverAccountBookMaps.get(hyShippingNote.getDriverId());
            HyPfAccount companyAccountBook = companyAccountBookMaps.get(hyShippingNote.getCompanyId());
            HyPfAccount carrierAccountBook = null;
            if ("1".equals(hyBill.getIzCarrierPay())) {
                carrierAccountBook = carrierAccountBookHashMaps.get(hyShippingNote.getCarrierId());
            }
            this.setBillPayData(payData, companyAccountBook, customerAccountBook, driverAccountBook, carrierAccountBook);
        }
    }

    @Transactional
    public void setBillRePayDatas(List<PayBillData> payDatas) {
        List<HyBill> bills = new ArrayList<>();
        for (PayBillData payData : payDatas) {
            HyBill hyBill = payData.getHyBill();
            hyBill.setPayStatus("5");
            bills.add(hyBill);
        }
        hyBillService.updateBatchById(bills);
    }
}
